package cn.modificator.relationtree

import android.animation.AnimatorInflater
import android.os.Bundle
import android.support.design.widget.AppBarLayout
import android.support.design.widget.FloatingActionButton
import android.support.v7.app.AppCompatActivity
import android.support.v7.view.menu.MenuItemImpl
import android.support.v7.widget.Toolbar
import android.view.Gravity
import android.view.Menu
import cn.modificator.relationtree.mainlist.MainItem
import cn.modificator.relationtree.mainlist.MainListItemBinder
import cn.modificator.relationtree.utils.recyclerListView
import cn.modificator.relationtree.widgets.DividerItemDecoration
import cn.modificator.relationtree.widgets.LinearLayoutManager
import cn.modificator.relationtree.widgets.RecyclerListView
import cn.modificator.relationtree.widgets.RecyclerView
import me.drakeet.multitype.MultiTypeAdapter
import org.jetbrains.anko.*
import org.jetbrains.anko.appcompat.v7.toolbar
import org.jetbrains.anko.design.coordinatorLayout
import org.jetbrains.anko.design.floatingActionButton
import org.jetbrains.anko.sdk25.coroutines.onClick


class MainActivity : AppCompatActivity() {

    var ankoComponent = MainUI();
    var adapter = MultiTypeAdapter()
    lateinit var list: RecyclerListView;
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        setContentView(R.layout.activity_main)
        ankoComponent.setContentView(this)
        setSupportActionBar(ankoComponent.toolbar)
//        list = RecyclerListView(this)
        list = ankoComponent.list
//        addContentView(list, ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
        initView()
    }

    fun initView() {
        list.adapter = adapter

        adapter.register(MainItem::class.java, MainListItemBinder())

        var dataList = arrayListOf<MainItem>()
        adapter.items = dataList

        dataList.add(MainItem(0, "啦啦啦", "电话：13333333333", "车型：不知道"))
        dataList.add(MainItem(0, "Moumoumou\nmoumoumou", "13333333333", "不知道"))
        dataList.add(MainItem(0, "啦啦啦", "电话：13333333333", "车型：不知道"))
        dataList.add(MainItem(0, "啦啦啦", "电话：13333333333", "车型：不知道"))
        dataList.add(MainItem(0, "啦啦啦", "电话：13333333333", "车型：不知道"))
        dataList.add(MainItem(0, "啦啦啦", "电话：13333333333", "车型：不知道"))
        dataList.add(MainItem(0, "啦啦啦", "电话：13333333333", "车型：不知道"))
        dataList.add(MainItem(0, "啦啦啦", "电话：13333333333", "车型：不知道"))
        dataList.add(MainItem(0, "啦啦啦", "电话：13333333333", "车型：不知道"))
        dataList.add(MainItem(0, "啦啦啦", "电话：13333333333", "车型：不知道"))
        dataList.add(MainItem(0, "啦啦啦", "电话：13333333333", "车型：不知道"))
        dataList.add(MainItem(0, "啦啦啦", "电话：13333333333", "车型：不知道"))
        dataList.add(MainItem(0, "啦啦啦", "电话：13333333333", "车型：不知道"))
        dataList.add(MainItem(0, "啦啦啦", "电话：13333333333", "车型：不知道"))
        dataList.add(MainItem(0, "啦啦啦", "电话：13333333333", "车型：不知道"))
        dataList.add(MainItem(0, "啦啦啦", "电话：13333333333", "车型：不知道"))
        adapter.notifyDataSetChanged()

        list.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)


                var translationY = ankoComponent.toolbar.translationY - dy
//                if (-translationY > ankoComponent.toolbar.height) {
                if (-translationY > 0) {
                    translationY = -ankoComponent.toolbar.height.toFloat()
                    ankoComponent.toolbar.isActivated = true
                } else if (translationY > 0) {
                    translationY = 0f
                    if (!list.canScrollVertically(-1))
                        ankoComponent.toolbar.isActivated = false
                }
//                ankoComponent.toolbar.translationY = translationY

            }
        })
    }

    fun addNewItem() {
        startActivity<CreateNewContentActivity>()
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menu?.let { menu ->
            menu.add(0, 1, 0, "排序").setIcon(R.drawable.ic_filter_list).setShowAsAction(MenuItemImpl.SHOW_AS_ACTION_ALWAYS)
        }

        return super.onCreateOptionsMenu(menu)
    }

    class MainUI : AnkoComponent<MainActivity> {
        lateinit var toolbar: Toolbar
        lateinit var list: RecyclerListView
        override fun createView(ui: AnkoContext<MainActivity>) = with(ui) {
            coordinatorLayout {
                //                toolbar = toolbar { title = "RelationTree" }
//                var listLParams = FrameLayout.LayoutParams(matchParent, matchParent)
//                listLParams.topMargin = dip(56)
                list = recyclerListView {
                    layoutManager = LinearLayoutManager(ui.ctx)
                    addItemDecoration(DividerItemDecoration(ui.ctx, DividerItemDecoration.VERTICAL))
                    setOnItemClickListener { view, position -> }
                }.lparams(matchParent, matchParent) {
                    topMargin = dip(56)
                    behavior = AppBarLayout.ScrollingViewBehavior()
                }
                list.setGlowColor(0xffdddddd.toInt())
                toolbar = toolbar {
                    title = "脉络"
                    backgroundColor = 0xffdddddd.toInt()
                    isActivated = true
                    stateListAnimator = AnimatorInflater.loadStateListAnimator(ui.ctx, R.animator.toolbar_raise)
                }.lparams(matchParent, wrapContent)
                floatingActionButton {
                    imageResource = R.drawable.ic_add_black_24dp
                    size = FloatingActionButton.SIZE_NORMAL
                    useCompatPadding = true
                    onClick { ui.owner.addNewItem() }
                }.lparams(wrapContent, wrapContent) {
                    gravity = Gravity.RIGHT or Gravity.BOTTOM
                    behavior = FloatingActionButton.Behavior()
                    bottomMargin = dip(25)
                    rightMargin = dip(25)
                }
            }


        }
    }
}

